﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Web;


namespace OT.Data
{
    public class Utilities
    {
        readonly static OTEntitiesme OTDb = new OTEntitiesme();

        public static void InsertErrorLog(Exception ex)
        {
            try
            {
                ErrorLog errorlog = new ErrorLog();
                string error = ex.ToString();
                char ch=':';
                int er=error.IndexOf(ch);
                errorlog.ErrorCode = ex.Source;
                errorlog.ErrorTitle = error.Substring(0, er);
               //  errorlog.NatureOfProblem = ex.Message.ToString();
                errorlog.Description = ex.StackTrace;
                errorlog.CreatedDate = DateTime.Now;
                OTDb.ErrorLogs.Add(errorlog);
                OTDb.SaveChanges();
            }
            catch (Exception e)
            {
                Console.Write("UnHandle Exception" + e);
            }
            
        }
        public static void SendEmail(string to,string sub,string body)
        {
            try
            {
                MailMessage mail1 = new MailMessage("omytestmail@gmail.com", to, sub, body);
                mail1.IsBodyHtml = true;
                SmtpClient smtp = new SmtpClient();
                smtp.Host = "smtp.gmail.com";
                smtp.Port = 587;
                smtp.UseDefaultCredentials = false;

                NetworkCredential crdntl = new NetworkCredential();
                crdntl.UserName = "omytestmail@gmail.com";
                crdntl.Password = "omytestmail123";

                smtp.Credentials = crdntl;

                smtp.DeliveryMethod = SmtpDeliveryMethod.Network;

                smtp.EnableSsl = true;

                smtp.Send(mail1);
            }
            catch (Exception e)
            {
                Utilities.InsertErrorLog(e);
            }

        }
        public static int GetLanguageID()
        {
            if (HttpContext.Current.Request.Cookies["LanguageID"] != null)
                return int.Parse(HttpContext.Current.Request.Cookies["LanguageID"].Value);
            return 1;
        }
    }
}